@echo off
:: 设置UTF-8编码
chcp 65001 > nul

:: 获取当前目录
set CURRENT_DIR=%~dp0

:: 切换到当前目录
cd /d "%CURRENT_DIR%"

echo 正在安装WebDetector...

:: 检查Python环境
python --version >nul 2>&1
if %errorlevel% neq 0 (
    echo 未检测到Python环境，请先安装Python 3.8或更高版本
    pause
    exit /b 1
)

:: 创建虚拟环境
echo 创建虚拟环境...
if not exist venv (
    python -m venv venv
)
call venv\Scripts\activate

:: 创建requirements.txt
echo playwright>=1.50.0 > requirements.txt
echo requests >> requirements.txt
echo paramiko >> requirements.txt
echo pymysql >> requirements.txt
echo pyinstaller >> requirements.txt

:: 安装依赖
echo 安装依赖包...
python -m pip install --upgrade pip
pip install -r requirements.txt

:: 安装playwright浏览器
echo 安装Playwright浏览器...
python -m playwright install chromium

:: 检查必要文件是否存在
if not exist stealth.min.js (
    echo 错误：缺少stealth.min.js文件
    pause
    exit /b 1
)
if not exist index.html (
    echo 错误：缺少index.html文件
    pause
    exit /b 1
)
if not exist main.js (
    echo 错误：缺少main.js文件
    pause
    exit /b 1
)

:: 编译程序
echo 开始编译程序...
pyinstaller --clean package.spec

:: 复制必要文件
echo 复制必要文件...
if not exist dist (
    mkdir dist
)
copy /Y stealth.min.js dist\
copy /Y index.html dist\
copy /Y main.js dist\

:: 创建目录
echo 创建必要目录...
if not exist dist\logs (
    mkdir dist\logs
)
if not exist dist\screen_shot (
    mkdir dist\screen_shot
)

echo 安装完成！程序位于dist目录下的WebDetector.exe
pause